using System;
using System.Collections.Generic;
using System.Text;
using jccc;

namespace testes
{
    public class lista_testes
    {

        private System.Collections.Generic.List<testes> l_testes;
        public lista_testes()
        { }

        public System.Collections.Generic.List<testes> devolve_lista_testes(int id_teste0)
        {
            jccc.bd db1 = new jccc.bd();
            l_testes = new System.Collections.Generic.List<testes>();
            testes testes1 = new testes();
            l_testes = db1.smart_devolve_lista(testes1, "id_teste", "=", id_teste0).ConvertAll<testes>(delegate(object value) { return (testes)value; });
            return l_testes;
        }

        public System.Collections.Generic.List<testes> devolve_lista_testes(bool todos)
        {
            jccc.bd db1 = new jccc.bd();
            l_testes = new System.Collections.Generic.List<testes>();
            testes testes1 = new testes();
            l_testes = db1.smart_devolve_lista(testes1, "1", "=", 1).ConvertAll<testes>(delegate(object value) { return (testes)value; });
            return l_testes;
        }

        public System.Collections.Generic.List<testes> devolve_lista_testes(bool todos, bool windows_client)
        {
            jccc.bd db1 = new jccc.bd(windows_client);
            l_testes = new System.Collections.Generic.List<testes>();
            testes testes1 = new testes();
            l_testes = db1.smart_devolve_lista(testes1, "1", "=", 1).ConvertAll<testes>(delegate(object value) { return (testes)value; });
            return l_testes;
        }
    }

    public class testes
    {
        public int id_teste;
        public string campoteste1;
        public decimal campoteste2;
        public DateTime campoteste3;
        public bool activa;

        public testes()
        {
            id_teste = 0;
            campoteste1 = "";
            campoteste2 = 0;
            campoteste3 = DateTime.Now;
            activa = false;
        }
        public testes(int id_teste0)
        {
            id_teste = id_teste0;
            campoteste1 = "";
            campoteste2 = 0;
            campoteste3 = DateTime.Now;
            activa = false;
        }
        public testes(int id_teste0, string campoteste10, decimal campoteste20, DateTime campoteste30, bool activa0)
        {
            id_teste = id_teste0;
            campoteste1 = campoteste10;
            campoteste2 = campoteste20;
            campoteste3 = campoteste30;
            activa = activa0;
        }

        public int id_teste_ { get { return id_teste; } set { id_teste = value; } }
        public string campoteste1_ { get { return campoteste1; } set { campoteste1 = value; } }
        public decimal campoteste2_ { get { return campoteste2; } set { campoteste2 = value; } }
        public DateTime campoteste3_ { get { return campoteste3; } set { campoteste3 = value; } }
        public bool activa_ { get { return activa; } set { activa = value; } }

        static void Main(string[] args)
        {

            //MSSQL database test
            jccc.bd db1 = new jccc.bd(); // connections string in app.config(windows client Aplication) or web.config(asp.net aplication) named: "conn"
            Console.Out.WriteLine("{0}", db1.executar_s("SELECT count(*) FROM testes;"));
            Console.In.ReadLine();

            //MySql database test
            jccc.bd_mysql db2 = new jccc.bd_mysql(2); // using connection string named: "conn2"
            Console.Out.WriteLine("{0}", db2.executar_s_int("SELECT count(*) FROM testes"));
            Console.In.ReadLine();

            testes t1 = new testes();

            //Reflection Test
            Console.Out.WriteLine("{0}", reflexao.devolveclass(t1));
            int y = reflexao.devolvenumerosdecampos(t1);
            Console.Out.WriteLine("{0}", y);
            Console.Out.WriteLine("{0}", reflexao.devolvenamespace(t1));
            for (int i = 0; i < y; i++)
            {
                Console.Out.WriteLine("{0}", reflexao.devolvenomecampo(t1, i));
                Console.Out.WriteLine("{0}", reflexao.devolvetipocampo(t1, i));
                Console.Out.WriteLine("{0}", reflexao.devolvevalorcampo(t1, i));
            }
            Console.Out.WriteLine("{0}", reflexao.devolvenomecampo(t1, 2));
            Console.Out.WriteLine("Valor antigo: {0}", reflexao.devolvevalorcampo(t1, 2));
            reflexao.definevalorcampo(t1, 2, (decimal.Parse("1,23")));
            Console.Out.WriteLine("{0}", reflexao.devolvenomecampo(t1, 2));
            Console.Out.WriteLine("Valor novo: {0}", reflexao.devolvevalorcampo(t1, 2));
            Console.In.ReadLine();


            //Testing insert's, updates and deletes, first record, next record, last record, previous record
            Console.Out.WriteLine("Insert {0}", db1.smart_insert(t1));
            Console.Out.WriteLine("Insert and Return antes{0} {1} depois{2}", t1.id_teste, db1.smart_insert_return_id(t1), t1.id_teste);
            t1.campoteste1 = "update";
            Console.Out.WriteLine("Update {0}", db1.smart_update(t1));
            Console.Out.WriteLine("Select {0}", db1.smart_select(t1));
            Console.Out.WriteLine("Delete {0}", db1.smart_delete(t1));
            Console.Out.WriteLine("First {0} {1}", db1.smart_select_primeiro(t1), t1.id_teste);
            Console.Out.WriteLine("Next {0} {1}", db1.smart_select_seguinte(t1), t1.id_teste);
            Console.Out.WriteLine("Last {0} {1}", db1.smart_select_ultimo(t1), t1.id_teste);
            Console.Out.WriteLine("Previous {0} {1}", db1.smart_select_anterior(t1), t1.id_teste);       
            
            //List's
            System.Collections.Generic.List<Object> list0 = db1.smart_devolve_lista(t1, "campoteste1", "=", "teste");
            Console.Out.WriteLine("{0}", list0.Count);
            for (int i = 0; i < list0.Count; i++)
            {
                testes t4;
                t4 = (testes)list0[i];
                Console.Out.WriteLine("{0} , {1} , {2} , {3} ", t4.id_teste, t4.campoteste1, t4.campoteste2, t4.campoteste3);
                Console.In.ReadLine();
            }
            Console.In.ReadLine();
            testes t5 = new testes(4);
            testes t6 = new testes(6);
            db1.smart_select(t5);
            db1.smart_select(t6);
            System.Collections.ArrayList lista = new System.Collections.ArrayList();
            lista.Add(t5);
            lista.Add(t6);
            t6.id_teste = 1223;
            Console.Out.WriteLine("{0} , {1} , {2} , {3} ", ((testes)(lista[0])).id_teste, ((testes)(lista[0])).campoteste1, ((testes)(lista[0])).campoteste2, ((testes)(lista[0])).campoteste3);
            Console.Out.WriteLine("{0} , {1} , {2} , {3} ", ((testes)(lista[1])).id_teste, ((testes)(lista[1])).campoteste1, ((testes)(lista[1])).campoteste2, ((testes)(lista[1])).campoteste3);
            Console.In.ReadLine();

            //Binding controls with data
            System.Web.UI.WebControls.DropDownList ddl1 = new System.Web.UI.WebControls.DropDownList();
            if (db1.smart_preenche_lista(ddl1, "SELECT id_teste,campoteste1 FROM testes", "id_teste", "campoteste1"))
            {
                for (int i = 0; i < ddl1.Items.Count; i++)
                {
                    Console.Out.WriteLine("value: {0} text: {1}", ddl1.Items[i].Value, ddl1.Items[i].Text);
                }
            }


            Console.In.ReadLine();

            
        }
    }

}
